package com.util;


import lombok.Data;

import java.io.Serializable;

@Data
public class Response<T> implements Serializable {
    private static final long serialVersionUID = 7026794286276703876L;

    /*
    * 返回信息
    * */
    private String msg;
    /*
     * 返回编码
     * */
    private Integer code;
    /*
     * 返回数据
     * */
    private Object data;
    /*
     * 返回总条数
     * */
    private Long total;
    /*
     * 返回boolean值
     * */
    private Boolean success;
    /*
     * 返回异常信息
     * */
    private String exception;
    /*
     * 返回错误编码
     * */
    private Integer errCode;

    public Response() {
    }

    public Response(String msg, Integer code, T data, Boolean success) {
        this.msg = msg;
        this.code = code;
        this.data = data;
        this.success = success;
        this.errCode = code;
    }

    public Response(String msg, T data, Boolean success) {
        this.msg = msg;
        this.data = data;
        this.success = success;
    }

    public Response(String msg, Integer code, T data, Boolean success, Long total) {
        this.msg = msg;
        this.code = code;
        this.data = data;
        this.success = success;
        this.total = total;
    }

    public Response(String msg, Boolean success) {
        this.msg = msg;
        this.success = success;
    }

    public Response(String msg, Integer code, T data, Boolean success, String exception) {
        this.msg = msg;
        this.code = code;
        this.data = data;
        this.success = success;
        this.exception = exception;
    }


    public static Response createSuccess(String msg) {
        return new Response(msg, BussiEnum.SCUESS.getCode(), null, true);
    }

    public static Response createSuccess() {
        return new Response(BussiEnum.SCUESS.getMsg(), BussiEnum.SCUESS.getCode(), null, true);
    }

    public static Response createSuccess(Object data) {
        return createSuccess(BussiEnum.SCUESS.getMsg(), data);
    }

    public static Response createSuccess(String msg, Object data) {
        return createSuccess(msg, BussiEnum.SCUESS.getCode(), data);
    }

    public static Response createSuccess(String msg, Integer code, Object data) {
        return new Response(msg, code, data, true);
    }
    public static Response createSuccess(String msg, Object data, String... args) {
        return createFormatMsg(msg, BussiEnum.SCUESS.getCode(), data, true, args);
    }

    public static Response createSuccess(String msg, String... args) {
        return createFormatMsg(msg, BussiEnum.SCUESS.getCode(),  null, true, args);
    }


    public static Response createSuccess(Object data, Long total) {
        return new Response("", BussiEnum.SCUESS.getCode(), data, true, total);
    }

    public Boolean isSuccess() {
        return this.success;
    }

    public static Response createError() {
        return new Response(BussiEnum.ERROR.getMsg(), BussiEnum.ERROR.getCode(),  null, false);
    }

    public static Response createError(String msg) {
        return createFormatMsg(msg, BussiEnum.ERROR.getCode(),  null, false);
    }

    public static Response createError(String msg, String... args) {
        return createFormatMsg(msg, BussiEnum.ERROR.getCode(),  null, false, args);
    }

    public static Response createError(String msg, Object data, String... args) {
        return createFormatMsg(msg, BussiEnum.ERROR.getCode(), data, false, args);
    }

    public static Response createError(String msg, Object data) {
        return new Response(msg, BussiEnum.ERROR.getCode(), data, false);
    }

    public static Response createError(String msg, Integer code, Object data) {
        return new Response(msg, code, data, false);
    }

    public static Response createError(String msg, Integer code) {
        return createFormatMsg(msg, code,  null, false);
    }

    public static Response createError(String msg, String exception) {
        return new Response(msg, BussiEnum.ERROR.getCode(),  null, false, exception);
    }

    private static Response createFormatMsg(String msg, Integer code, Object data, Boolean success, String... args) {
        return new Response(String.format(msg, args), code, data, success);
    }
}
